package com.example.yls.demoa;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.easy_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSimpleDialog();

            }
        });
        findViewById(R.id.cities_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setCityDialog();

            }
        });
        findViewById(R.id.sex_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSexChoice();

            }
        });

        findViewById(R.id.hobbies_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setMultiChoice();


            }
        });


        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setShowSimpleDialog();

            }
        });
    }
    private void setSimpleDialog() {

        final boolean isChecked[]=new boolean[6];

        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("对话框")
                .setMessage("Are you sure?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"position:" + i, Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"Negative:" + i, Toast.LENGTH_SHORT).show();
                    }
                })
                .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"Neutral:" + i, Toast.LENGTH_SHORT).show();
                    }
                })
                .create()//公共部分
                .show();

    }
    private void setCityDialog() {
        final String[] cities = {"广州", "天河", "花都", "东莞", "深圳"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("选择一个城市")
                .setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int h) {
                        Toast.makeText(MainActivity.this, "Items:" + h, Toast.LENGTH_SHORT).show();

                    }
                })
                .create()//公共部分
                .show();
    }
    private void setSexChoice() {
        final String[] sex = {"男", "女", "伪娘"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择你的性别")
                .setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int sex) {

                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .create()//公共部分
                .show();

    }
    private void setMultiChoice() {
        final String[] hobbies={"篮球","足球","游戏","听歌","打代码"};
        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请选择你的爱好")
                .setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener() {

                    StringBuffer sb=new StringBuffer(100);
                    @Override

                    public void onClick(DialogInterface dialogInterface, int b, boolean isChecked) {
                        if(isChecked){
                            sb.append(hobbies[b]+",");

                        }
                        Toast.makeText(MainActivity.this,"爱好为:"+sb.toString(),
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })

                .create()//公共部分
                .show();

    }
    private void setShowSimpleDialog(){
        final EditText editText1=new EditText(MainActivity.this);
        final EditText editText2=new EditText(MainActivity.this);

        View view= LayoutInflater.from(MainActivity.this)
                .inflate(R.layout.dialog_input,null,false);

        final  EditText nameEdit=view.findViewById(R.id.username);
        final  EditText passwordEdit=view.findViewById(R.id.password);

        new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.mipmap.ic_launcher)
                .setTitle("请输入用户名和密码:")
                .setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //将输入的用户名和密码打印出来
                        String name = nameEdit.getText().toString();
                        String pd = passwordEdit.getText().toString();

                        Toast.makeText(MainActivity.this,"用户名:" +  name +"密码:"+pd, Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this,"Negative:" + i, Toast.LENGTH_SHORT).show();
                    }
                })
                .create()//公共部分
                .show();


    }


}
